package com.choosefine.it.manager.controller;

import com.choosefine.it.common.utils.StringUtils;
import com.choosefine.it.manager.model.dto.ProjectInfoDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.choosefine.it.base.BaseController;
import com.choosefine.it.basedata.model.FinishPhoto;
import com.choosefine.it.basedata.model.ProjectInfo;
import com.choosefine.it.basedata.service.FinishPhotoService;
import com.choosefine.it.basedata.service.ProjectInfoService;
import com.choosefine.it.common.def.ResultCode;

/**
 * @author chenjian
 * @date 2016-11-25 下午4:01:29
 */
@RestController
@RequestMapping("/manager/managerProject")
@Api(value = "项目经理管理的项目API", description = "陈健")
public class ManagerProjectInfoController extends BaseController {
	@Autowired
	private ProjectInfoService projectInfoService;
	@Autowired
	private FinishPhotoService finishPhotoService;

	@ApiOperation("项目经理管理的项目")
	@ApiImplicitParams({ @ApiImplicitParam(paramType = "path", name = "userCode", dataType = "String", required = true, value = "用户code"), })
	@RequestMapping(value = "/{userCode}", method = RequestMethod.GET)
	public String findProjectInfoByManager(
			@PathVariable("userCode") String userCode) {
		String result = null;
		if (StringUtils.isNotBlank(userCode)) {
			List<ProjectInfo> projectInfoList = new ArrayList<>();
			// 用户code查询项目
			projectInfoList = projectInfoService
					.findProjectInfoByManager(userCode);
			if (projectInfoList.size() > 0) {
				List<ProjectInfoDTO> projectInfoDTOList = new ArrayList<ProjectInfoDTO>();
				for (ProjectInfo projectInfo : projectInfoList) {
					ProjectInfoDTO projectInfoDTO = new ProjectInfoDTO();
					// 项目code查询已完工项目图片
					List<FinishPhoto> finishPhotoList = finishPhotoService
							.findPhotoByProjectCode(projectInfo
									.getProjectCode());
					projectInfoDTO.setFinishPhotoList(finishPhotoList);
					projectInfoDTO.setProjectInfo(projectInfo);
					projectInfoDTOList.add(projectInfoDTO);
				}
				result = responseArraySuccess(projectInfoDTOList,
						ResultCode.SUCCESS, ResultCode.MSG_SUCCESS);
			}else{
				result=responseFail(ResultCode.FAILED, ResultCode.MSG_DATA_ISEMPTY);
			}
		}else{
			result=responseFail(ResultCode.FAILED, ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}

}
